const controllers = require('../../controllers/controllerIndex.js').controller;
Component({
  properties: {
    isshow:{
      type:Boolean,
      default:false
    },
    modeltype:{
      type:Number,
      default:1,
      observer(newval,oldval){
          this.setData({
            shopTypeListid:'',
            shopTypeListindex:0,
            selectByTypeIddata:[],
            selectByTypeIdindex:0,
            selectGoodsTempdata:[],
            selectGoodsTempindex:-1,
            goodsTempDetaildata:'',
            selectGoodsTempid:'',
            selectGoodsTempitem:'',
          })
        // if(newval!=oldval){
          this.shopTypeList()
        // }
      }
    },
  },
  data: {
    shopTypeListindex:0,
    shopTypeList:[],
    shopTypeListid:'',
    selectByTypeIddata:[],
    selectByTypeIdindex:0,
    selectByTypeIdid:'',
    selectGoodsTempdata:[],
    selectGoodsTempindex:-1,
    selectGoodsTempid:'',
    selectGoodsTempitem:'',
    isloading:true,
    goodsTempDetaildata:''
  },
  ready() {
      // this.shopTypeList()
  },
  methods: {
    //右侧内容
    shopTypeList(){
      var that =this;
      controllers.shopTypeList().then(res =>{
        that.setData({
          shopTypeListdata: res.data,
          shopTypeListid:res.data[0].id
        })
        that.selectByTypeId()
      })
    },
    // shopInfo(){
    //   var that =this;
    //   controllers.shopInfo({shopId:wx.getStorageSync('shopId')}).then(res =>{
    //     that.setData({
    //       shopTypeListdata: [res.data]
    //     })
    //     that.selectLabel()
    //   })
    // },
    selectByTypeId(){
      var that =this;
      controllers.selectByTypeId(that.data.shopTypeListid).then(res =>{
        if(res.data.length>0){
          that.setData({
            selectByTypeIddata: res.data,
            selectByTypeIdid:res.data[this.data.selectByTypeIdindex].id
          })
        
          that.selectGoodsTemp()
        }
        
      })
    },
    // selectLabel(){
    //   controllers.selectLabel({
    //     type: 1,
    //     shopId:wx.getStorageSync('shopId'),
    //   }).then(res =>{
    //     this.setData({
    //       selectByTypeIddata:res.data,
    //       selectByTypeIdid:res.data[this.data.selectByTypeIdindex].id
    //     })
    //     this.selectGoodsTemp()
    //   })
    // },
    selectGoodsTemp(){
      var that =this;
      controllers.selectGoodsTemp({shopId:wx.getStorageSync('shopId'),labelId:that.data.selectByTypeIdid,type:this.data.modeltype}).then(res =>{
        let lists=[]
        for(let item of res.data){
          // if(item.state==1){
            lists.push(item)
          // }
        }
        that.setData({
          selectGoodsTempdata:lists,
          isloading:false
        })
      }).catch(res=>{
        that.setData({
          isloading:false
        })
      })
    },
    getshopTypeList(e){
      let id=e.currentTarget.dataset.id;
      let index=e.currentTarget.dataset.index;
      this.setData({
        shopTypeListid:id,
        shopTypeListindex:index,
        selectByTypeIddata:[],
        selectByTypeIdindex:0,
        selectGoodsTempdata:[],
        selectGoodsTempindex:-1,
        goodsTempDetaildata:''
      })
      this.selectByTypeId()
    },
    getselectByTypeId(e){
      let id=e.currentTarget.dataset.id;
      let index=e.currentTarget.dataset.index;
      this.setData({
        selectByTypeIdid:id,
        selectByTypeIdindex:index,
        selectGoodsTempdata:[],
        selectGoodsTempindex:-1,
        goodsTempDetaildata:''
      })
      this.selectGoodsTemp()
    },
    getselectGoodsTemp(e){
      let item=e.currentTarget.dataset.item;
      let index=e.currentTarget.dataset.index;
      this.setData({
        selectGoodsTempitem:item,
        selectGoodsTempindex:index,
        goodsTempDetaildata:''
      })
      this.goodsTempDetail()
    },
    goodsTempDetail(){
      controllers.goodsTempDetail({
        shopId:wx.getStorageSync('shopId'),
        goodsTempId:this.data.selectGoodsTempitem.id,
        goodsTemplateId:this.data.selectGoodsTempitem.id,
      }).then(res => {
        this.setData({
          goodsTempDetaildata:res.data
        })
      })
    },
    cancel(){
      this.setData({
        isshow:false
      })
    },
    confirm(){
      let goodsTempDetaildata=this.data.goodsTempDetaildata;
      if(!goodsTempDetaildata){
        wx.showToast({
          title: '请选择模板！',
          icon: 'none',
          duration: 2000
        })
        return
      }
      controllers.getSetByParentId({
        shopId:wx.getStorageSync('shopId'),
        labelId:goodsTempDetaildata.labelId,
        type:3,
        sessionId:wx.getStorageSync('token')

      }).then(res => {
          if(res.code==1){

            let data = {};

            data.shopId=wx.getStorageSync('shopId');
            data.labelId=res.data.id;
            data.state=goodsTempDetaildata.state;
            data.rewardState=goodsTempDetaildata.rewardState;
            data.integralState=goodsTempDetaildata.integralState;
            data.vip=goodsTempDetaildata.vip;
            data.type=goodsTempDetaildata.type;
            data.heat=goodsTempDetaildata.heat;
            data.oldPrice=goodsTempDetaildata.oldPrice;
            data.price=goodsTempDetaildata.price;
            data.name=goodsTempDetaildata.goodsName;
            data.content=goodsTempDetaildata.content || '';
            data.image=goodsTempDetaildata.image;
            data.integral=goodsTempDetaildata.integral || 0;
            data.rewardProportion=goodsTempDetaildata.rewardProportion || '';
            data.stock=goodsTempDetaildata.stock || '';
            data.warnCount=goodsTempDetaildata.warnCount || '';
            data.vipPrice=goodsTempDetaildata.vipPrice;
            data.rewardPrice=goodsTempDetaildata.rewardPrice || '';
            if(goodsTempDetaildata.detailsImage && goodsTempDetaildata.detailsImage.length>0){
              var detailsImage=[]
              for(let item of goodsTempDetaildata.detailsImage){
                detailsImage.push(item.image)
              }
              data.detailImgs=detailsImage.join(',') || '';
            }

            if(goodsTempDetaildata.rotationImage && goodsTempDetaildata.rotationImage.length>0){
              var rotationImage=[]
              for(let item of goodsTempDetaildata.rotationImage){
                rotationImage.push(item.image)
              }
              data.rotationImgs=rotationImage.join(',') || '';
            }


            controllers.saveGoods(data).then(res => {
              wx.showToast({
                title: '保存成功！',
                icon: 'success',
                duration: 2000
              })
              var that=this;
              setTimeout(res => {
                that.triggerEvent('loaddata');
                that.setData({
                  isshow:false
                })
              })
            });

          }
      });

    
    },
  }
})